namespace BassettData.Robots.Runner
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// Records the result of a robot task run.
    /// </summary>
    public class RunResult
    {
        /// <summary>
        /// Records the exceptions thrown during execution.
        /// </summary>
        private List<Exception> exceptionsThrown;

        /// <summary>
        /// Initializes a new instance of the <see cref="RunResult"/> class.
        /// </summary>
        public RunResult()
        {
            this.exceptionsThrown = new List<Exception>();
        }

        /// <summary>
        /// Gets a value indicating whether all tasks ran to completion.
        /// </summary>
        /// <value>
        /// <c>True</c> if [all tasks run to completion]; otherwise, <c>False</c>.
        /// </value>
        public bool AllTasksRanToCompletion
        {
            get
            {
                return this.exceptionsThrown.Count == 0;
            }
        }

        /// <summary>
        /// Records the exception.
        /// </summary>
        /// <param name="ex">The ex.</param>
        public void RecordException(Exception ex)
        {
            this.exceptionsThrown.Add(ex);
        }

        /// <summary>
        /// Summarises the run result.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            if (this.AllTasksRanToCompletion)
            {
                return "All tasks ran to completion successfully.";
            }

            StringBuilder result = new StringBuilder("There were errors running the task:" + Environment.NewLine);
            foreach (var exception in this.exceptionsThrown)
            {
                result.AppendLine(exception.ToString());
            }

            return result.ToString();
        }
    }
}
